package com.zw.yzk.kabu.mapper;

import com.zw.yzk.kabu.data.network.entity.GoodsOrderDetailsEntity;
import com.zw.yzk.kabu.data.network.task.GoodsOrderTask;
import com.zw.yzk.kabu.model.GoodsOrderDetailsModel;
import com.zw.yzk.kabu.model.GoodsOrderModel;
import com.zw.yzk.kabu.utils.CalculateUtils;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by zhanwei on 2017/3/14.
 */

public class GoodsOrderDetailsMapper {

    @Inject
    public GoodsOrderDetailsMapper() {
    }

    public GoodsOrderDetailsModel transfer(GoodsOrderDetailsEntity entity) {
        GoodsOrderDetailsModel model = new GoodsOrderDetailsModel();
        model.setCreateTime(entity.result.createTime);
        model.setOrderType(entity.result.orderType);
        model.setOrderId(entity.result.orderId);
        model.setReceivePhone(entity.result.receivePhone);
        model.setReceiverAddress(entity.result.receiverAddress);
        model.setTotalPrice(entity.result.totalPrice);
        model.setDeadTime(entity.result.deadTime);
        model.setShopId(entity.result.shopId);
        model.setReceiveName(entity.result.receiveName);
        model.setReceiverInfoId(entity.result.receiverInfoId);
        model.setUsrId(entity.result.usrId);
        model.setShopOwnerId(entity.result.shopOwnerId);
        model.setShopName(entity.result.shopName);
        model.setFreight(entity.result.freight);
        model.setLogisticId(entity.result.logisticId);
        model.setGoodsList(transfer(entity.result.goodsList));

        int count = 0;
        for (GoodsOrderModel.Goods g : model.getGoodsList()) {
            count += g.getCount();
        }
        model.setGoodsCount(count);

        switch (entity.result.orderType) {
            case GoodsOrderTask.GOODS_ORDER_ALL:
                break;
            case GoodsOrderTask.GOODS_ORDER_COMPLETE:
                model.setOrderTypeStr("已完成");
                break;
            case GoodsOrderTask.GOODS_ORDER_UNDELIVER:
                model.setOrderTypeStr("待发货");
                break;
            case GoodsOrderTask.GOODS_ORDER_UNPAIED:
                model.setOrderTypeStr("待付款");
                break;
            case GoodsOrderTask.GOODS_ORDER_UNRECEIVE:
                model.setOrderTypeStr("待收货");
                break;
            case GoodsOrderTask.GOODS_ORDER_BACK:
                model.setOrderTypeStr("退货");
                break;
            default:
                break;
        }
        return model;
    }

    public GoodsOrderModel.Goods transfer(GoodsOrderDetailsEntity.Goods goods) {
        GoodsOrderModel.Goods model = new GoodsOrderModel.Goods();
        model.setId(goods.goodsId);
        model.setOriginalPrice(goods.originalPrice);
        model.setCount(goods.count);
        model.setProperty(goods.property);
        model.setImage(goods.image);
        model.setDiscountedPrice(goods.discountedPrice);
        model.setPrice(CalculateUtils.getRealPrice(goods.originalPrice, goods.discountedPrice));
        model.setName(goods.goodsName);

        return model;
    }

    public List<GoodsOrderModel.Goods> transfer(List<GoodsOrderDetailsEntity.Goods> goodsList) {
        List<GoodsOrderModel.Goods> modelList = new ArrayList<>();
        if(goodsList == null || goodsList.isEmpty()) {
            return modelList;
        }
        for(GoodsOrderDetailsEntity.Goods goods : goodsList) {
            modelList.add(transfer(goods));
        }
        return modelList;
    }
}
